/*
 * BlokjesInruiler.java
 *
 * Created on 19 april 2008, 20:06
 */
package client.gui;

import client.gui.controls.scrabble.Blokje;
import client.communication.ClientCommunication;
import client.gui.ScrabbleClient;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JButton;

/**
 *
 * @author  Liesbeth
 */
public class BlokjesInruiler extends javax.swing.JDialog {

    private JButton knop;
    private ScrabbleClient parent;

    /** Creates new form BlokjesInruiler */
    //alle letters meegeven die op het plankje liggen
    //hieruit kan de speler dan kiezen welke blokjes hij effectief wil inruilen
    public BlokjesInruiler(Blokje[] blokjes, ScrabbleClient parent) {
        initComponents(); 
        this.toFront();
        this.parent = parent;
        DefaultListModel model = new DefaultListModel();
        for (int i = 0; i < blokjes.length; i++) {
            if(blokjes[i].isBlancoBlokje()){
                model.addElement("?");
            }else{
                model.addElement(blokjes[i].getLetter());
            }
        }
        liLetters.setModel(model);
        pack();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        liLetters = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        btnRuil = new javax.swing.JButton();

        jScrollPane1.setViewportView(liLetters);

        jLabel1.setText("Te wisselen letters: ");

        btnRuil.setText("Letters inruilen");
        btnRuil.setToolTipText("Door CTRL in te drukken kan u verschillende letters selecteren");
        btnRuil.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRuilActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnRuil)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE)
                    .addComponent(btnRuil))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    private void btnRuilActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRuilActionPerformed
        String[] letters = new String[liLetters.getSelectedValues().length];
        int i = 0;
        for (Object o : liLetters.getSelectedValues()) {
            letters[i] = (String) o;
            i++;
        }
        //parent.setOmTeRuilenLetters(letters);
        ArrayList<String> arlletters = new ArrayList<String>();
        for (String s : letters) {
            arlletters.add(s);
            boolean blanco = false;
            if (s.equals("?")) {
                blanco = true;
            }
            ClientCommunication.getInstance().getPlankje().verwijderBlokje(s, blanco);
        }
        ClientCommunication.getInstance().geefLettersTerug(arlletters);
        String gameNaam = ClientCommunication.getInstance().getGameNaam();
        ClientCommunication.getInstance().aanvraagLetters(arlletters.size(), parent.getGebruiker().getNickname(), gameNaam);
        ClientCommunication.getInstance().geefBeurtDoor();
        dispose();
        
}//GEN-LAST:event_btnRuilActionPerformed

    public boolean toon() {
        knop = null;
        setVisible(true);
        this.setLocationRelativeTo(parent);
        return knop == btnRuil;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRuil;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList liLetters;
    // End of variables declaration//GEN-END:variables
}
